<template>
  <div class="templateDetailWrap1">
    <div class="searchWrap">
      <span class="searchFont">企业选择：</span>
      <el-radio-group v-model="checkEnterPriseNo" size="mini"  @change="getExamineInfo">
        <el-radio-button v-for="item in $store.state.companyInfos" :label="item.enterpriseNo" :key="item.enterpriseNo">{{item.enterpriseName}}</el-radio-button>
      </el-radio-group>
      <div v-if="$store.state.btnMenuIds.indexOf(78)>-1" style="float:right;" >
        <returnBtn v-show="agencyId" @getExamineInfo="getExamineInfo" @handleClose="handleClose" :taskId="taskId" :agencyId="agencyId"></returnBtn>
      </div>
    </div>
    <div class="tableContainer">
      <div class="tableInner" ref="tableInner">
        <el-table
          ref="multipleTable"
          :data="rows"
          stripe
          :height="tableHeight"
          @selection-change="changeFun"
          :span-method="objectSpanMethod"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column
            label="指标类别"
            prop="classifyName"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="类别分值"
            prop="classifyScore"
            show-overflow-tooltip
          >

          </el-table-column>
          <el-table-column
            prop="itemsName"
            label="指标项"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="standardScore"
            label="指标分值"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="指标描述"
            prop="itemsDesc"
            show-overflow-tooltip>

          </el-table-column>
          <el-table-column
            label="评分标准"
            prop="scoreRule"
            show-overflow-tooltip
            >
            <template slot-scope="scope">
              <div style="float: left;">
                {{scope.row.scoreRule}}
              </div>

            </template>
          </el-table-column>
          <el-table-column
            label="最终得分"
            prop="finalScore"
            show-overflow-tooltip>

          </el-table-column>
          <el-table-column
            label="是否参与考核"
            prop="finalScore"
            show-overflow-tooltip>
            <template slot-scope="scope">
              {{scope.row.hasExamine==1?"参与考核":"不参与考核"}}
            </template>
          </el-table-column>
          <el-table-column
            label="附件管理"
            prop="hasAttachment"
            show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="!scope.row.hasAttachment">无附件</span>
            <span style="cursor: pointer;color: #3975FE;"  @click="downLoadXml(scope.row.relativeAddr)" v-if="scope.row.hasAttachment">{{getFileName(scope.row.attachmentAddr)}}</span>
          </template>
          </el-table-column>

        </el-table>

      </div>
    </div>
  </div>
</template>

<script>
  import returnBtn from "./dialog/returnBtn.vue"
  export default {
    props:["templateId","agencyId","taskId"],
    data() {
      return {
        rows: [],
        checkEnterPriseNo:"075501",
        spanArr: [],
        multipleSelection: [],
        templateInfoList: [],
        tableHeight: 400,
        templateName: "",
        backAgree: "",
        totalScore:0,
        noScore:false,
        dialogVisible:false
      }
    },
    components: {returnBtn},
    created(){
      setTimeout(()=>{
        this.getExamineInfo()
      },500)

    },
    mounted(){
      setTimeout(() => {
        //表格滚动
        this.tableHeight = document.querySelector(".taskManageWrapWrap").clientHeight - 240
      },100)
    },
    methods: {
      //下载模板
      downLoadXml(downloadPath){
        window.location.href = downloadPath
      },
      handleClose(){

        this.$emit("getTempalteInfo")

      },
      confirmExamineReult(){
        this.axios.post(process.env.API_HOST + `/examine/updateAgencyDetail`, {
          taskId: this.taskId,
          agencyId: this.$store.state.agencyId,
          backAgree:this.backAgree
        })
          .then(response => {
            let result = response.data
            this.dialogVisible = false
            if (result.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });

            }
          })
      },
      //      数组去重
      arrayUnique(arr, name) {
        var hash = {};
        return arr.reduce(function (item, next) {
          hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
          return item;
        }, []);
      },

      //全选
      changeFun(val) {

        this.multipleSelection = val
        this.totalScore=0
        this.templateInfoList=[]
        //组装数组
        let newArr=this.arrayUnique(this.multipleSelection,"classifyId")
        newArr.forEach(item=>{
          this.templateInfoList.push({
            classifyId:item.classifyId,
            classifyName:item.classifyName,
            itemList:[]
          })
        })
        this.noScore=false
        this.multipleSelection.forEach(item=>{
          this.totalScore+=this.toDecimal(item.standardScore)
          if(!item.standardScore){
            this.noScore=true
          }
          this.templateInfoList.forEach(item1=>{
            if(item1.classifyId==item.classifyId){
              item1.itemList.push(item)
            }
          })
        })
      },

      //获取所有信息
      getExamineInfo(){
        if(this.agencyId==""){
          return
        }
        this.axios.post(process.env.API_HOST + `/examine/selectAgencyDetail`, { templateId:this.templateId,agencyId:this.agencyId,taskId: this.taskId})
          .then(response => {
            let result = response.data
            if(result.code==200){
              this.rows=[]

              result.result.forEach(item=>{
                if(item.enterpriseNo==this.checkEnterPriseNo){
                  item.classifyList.forEach(item1=>{
                    item1.itemList.forEach(item2=>{
                      this.rows.push({...item2,classifyId:item1.classifyId,classifyName:item1.classifyName,classifyScore:""})
                    })
                  })
                }
              })
              if(this.rows.length>0){
                  this.totalScore=0
                this.spanArr=this.getSpanArr(this.rows,"classifyName")
                this.rows.forEach((item,key)=>{
                  this.totalScore+=this.toDecimal(item.standardScore)
                  this.getclassifyScore(key)
                })
              }else {
                this.rows=[]
              }

            }
          })

      },
      objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (columnIndex === 0||columnIndex === 1) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col
          };
        }
      },
      getclassifyScore(index){
        let sub=0,isPass=true,classifyScore=0
        this.spanArr.forEach(item=>{
          sub+=item
          if(isPass&&sub>index){
            isPass=false
            const sliceArr = this.rows.slice(sub-item,sub)
            sliceArr.forEach(item=>{
              classifyScore+=this.toDecimal(item.standardScore)
            })
            sliceArr.forEach(item=>{
              item.classifyScore=classifyScore
            })
          }
        })

      },
    },
    computed: {

    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .templateDetailWrap1
    padding-top 15px
    border-radius 5px
    height 100%
    .detailHeader
      background-color: #ECF5FF;
      text-align: center;
      color: #3975FE;
      line-height: 40px;
      font-size: 20px;
      font-weight: bold;
      span
        font-weight bold
      .iconWrap
        float: right;
        margin-right: 15px;
        color: white;
        .iconfont
          color: #3975FE;
          &:hover
            opacity 0.8
    .searchWrap
      padding 0 20px
      white-space nowrap
      overflow hidden
      font-size 14px
      line-height 28px
      .el-radio-button
        .el-radio-button__inner
          padding 4px 20px !important
          margin 0 10px
          border none !important
          color: #9E9E9E;;
      .is-active
        .el-radio-button__inner
          color: #fff;
          background-color: $co1
          border-radius 10px !important
      .searchFont
        margin-left 5px
        font-size 14px
        color #50545D
        font-family SourceHanSansCN-Medium
    .tableContainer
      padding 15px 20px 100px 20px
      background-color white
      border-radius 5px
      margin 0 auto
      .tableInner //表格滚动
        width 100%
        overflow-y auto

</style>
